<template>
  <div class="guns-layout">
    <div class="guns-layout-content">
      <div class="guns-layout">
        <div class="guns-layout-content-application">
          <div class="content-mian">
            <div class="content-mian-header">
              <div class="header-content">

                <div class="header-content-left">
                  <a-space :size="16">
                    <a-input v-model:value="where.searchText" placeholder="名称（回车搜索）" @pressEnter="reload"
                      class="search-input">
                      <template #prefix>
                        <icon-font iconClass="icon-opt-search"></icon-font>
                      </template>
                    </a-input>
                    <a-button class="border-radius" @click="clear">重置</a-button>
                  </a-space>
                </div>

                <div class="header-content-right">
                  <a-space :size="16">
                    <a-button type="primary" class="border-radius" @click="openAddEdit()"
                      v-permission="['ADD_SAMPLE']"><plus-outlined />新建</a-button>
                    <a-dropdown>
                      <template #overlay>
                        <a-menu @click="moreClick">
                          <a-menu-item key="1">
                            <icon-font iconClass="icon-opt-zidingyilie" color="#60666b"></icon-font>
                            <span>自定义列</span>
                          </a-menu-item>
                          <div v-permission="['DELETE_SAMPLE']">
                            <a-menu-item key="2">
                              <icon-font iconClass="icon-opt-shanchu" color="#60666b"></icon-font>
                              <span>批量删除</span>
                            </a-menu-item>
                          </div>
                        </a-menu>
                      </template>
                      <a-button class="border-radius">
                        更多
                        <small-dash-outlined />
                      </a-button>
                    </a-dropdown>
                  </a-space>
                </div>
              </div>
            </div>
            <div class="content-mian-body">
              <div class="table-content">
                <common-table :columns="columns" :where="where" rowId="id" ref="tableRef"
                  url="/api/service/sampleData/page">
                  <template #bodyCell="{ column, record }">
                    <!-- 姓名 -->
                    <template v-if="column.dataIndex == 'name'">
                      <a @click="openAddEdit(record)">{{ record.name }}</a>
                    </template>
                    <template v-if="column.dataIndex == 'qrBase64'">
                      <img :src="record.qrBase64" alt="" class="appIconWrapper" @click="handlePreviewPhoto(record)"/>
                    </template>
                    <!-- 操作 -->
                    <template v-if="column.key == 'action'">
                      <a-space :size="16">


                        <icon-font iconClass="icon-opt-bianji" font-size="24px" title="编辑" color="#60666b"
                          v-permission="['EDIT_SAMPLE']" @click="openAddEdit(record)" />
                        <icon-font iconClass="icon-opt-shanchu" font-size="24px" title="删除" color="#60666b"
                          v-permission="['DELETE_SAMPLE']" @click="remove(record)" />
                      </a-space>
                    </template>
                  </template>
                </common-table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 自定义列 -->
    <Custom v-model:visible="isShowCustom" v-if="isShowCustom" :data="columns" @done="val => (columns = val)"
      :fieldBusinessCode="fieldBusinessCode" />

    <!-- 新增编辑弹框 -->
    <SampleAddEdit v-model:visible="showEdit" v-if="showEdit" :data="current" @done="reload" />
    <!-- 图标预览弹框 -->
    <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible = false" style="width: 800px;">
      <div style="display: flex;flex-direction: row;">
        <img alt="example" style="width: 100%" :src="previewData.qrBase64" />
      <div>
        <a-card>
        <a-row>
          <a-col :span="24">
            <p><strong>姓名:</strong> {{ previewData.name }}</p>
          </a-col>
          <a-col :span="24">
            <p><strong>日期:</strong> {{ previewData.date }}</p>
          </a-col>
          <a-col :span="24">
            <p><strong>keywords:</strong> {{ previewData.samplesKeywords }}</p>
          </a-col>
        </a-row>
      </a-card>
      </div>
      </div>
      
    </a-modal>
  </div>
</template>

<script setup name="AuthRole">
import { SampleApi } from './api/SampleApi';
import { ref, createVNode, onMounted, computed } from 'vue';
import { message, Modal } from 'ant-design-vue/es';
import SampleAddEdit from './components/sample-add-edit.vue';
import { useUserStore } from '@/store/modules/user';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { CustomApi } from '@/components/common/Custom/api/CustomApi';
// 是否展示图片预览
const previewVisible = ref(false);
// 图片地址
const previewData = ref(null);
// 点击预览图片
const handlePreviewPhoto = record => {
  previewData.value = record;
  previewVisible.value = true;
};

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 48,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'name',
    title: '姓名',
    ellipsis: true,
    width: 200,
    isShow: true
  },
  {
    dataIndex: 'date',
    title: '日期',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'time',
    title: '时间',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'samplesType',
    title: '类型',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'samplesKeywords',
    title: 'keywords',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'samplesAmount',
    title: '样本数量',
    width: 100,
    isShow: true
  },
  // {
  //   dataIndex: 'storageLocation',
  //   title: '存储位置',
  //   width: 100,
  //   isShow: true
  // },
  {
    dataIndex: 'conditionText',
    title: '条件',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'fileAddress',
    title: '文件地址',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'createTime',
    title: '创建时间',
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'qrBase64',
    title: '二维码',
    width: 100,
    isShow: true
  },
  {
    key: 'action',
    title: '操作',
    width: 100,
    isShow: true
  }
]);
// ref
const tableRef = ref(null);

// 搜索条件
const where = ref({
  searchText: ''
});
// 是否显示自定义列
const isShowCustom = ref(false);
// 当前行数据
const current = ref(null);
// 是否显示新增编辑弹框
const showEdit = ref(false);
// 业务标识的编码
const fieldBusinessCode = ref('APP_TABLE');

const userStore = useUserStore();

// 是否禁用
const disabled = computed(() => {
  if (userStore.authorities.find(item => item == 'UPDATE_USER_STATUS')) {
    return false;
  }
  return true;
});

onMounted(() => {
  getColumnData();
});

// 获取表格配置
const getColumnData = () => {
  CustomApi.getUserConfig({ fieldBusinessCode: fieldBusinessCode.value }).then(res => {
    if (res.tableWidthJson) {
      columns.value = JSON.parse(res.tableWidthJson);
    }
  });
};

// 更多点击
const moreClick = ({ key }) => {
  if (key == '1') {
    isShowCustom.value = true;
  } else if (key == '2') {
    batchDelete();
  }
};

// 点击搜索
const reload = () => {
  tableRef.value.reload();
};

// 清除搜索条件
const clear = () => {
  where.value.searchText = '';
  reload();
};

// 新增编辑点击
const openAddEdit = record => {
  current.value = record;
  showEdit.value = true;
};

// 删除单个
const remove = record => {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的数据吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await SampleApi.delete({ id: record.id });
      message.success(res.message);
      reload();
    }
  });
};

// 批量删除
const batchDelete = () => {
  if (tableRef.value.selectedRowList && tableRef.value.selectedRowList.length == 0) {
    return message.warning('请选择需要删除的数据');
  }
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的数据吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await SampleApi.batchDelete({ appIdList: tableRef.value.selectedRowList });
      message.success(res.message);
      reload();
    }
  });
};


</script>

<style scoped lang="less">
.appIconWrapper {
  width: 22px;
  height: 22px;
}
</style>
